﻿using System;
using System.Data;
using System.Text;
using System.Data.OleDb;
using ihongma.DBUtility;
using ihongma.Common;//Please add references
namespace ihongma.DAL
{
	/// <summary>
	/// 数据访问类:Order
	/// </summary>
	public partial class Order
	{
		public Order()
		{}
		#region  Method

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperOleDb.GetMaxID("id", "dt_Order"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from dt_Order");
			strSql.Append(" where id=@id");
			OleDbParameter[] parameters = {
					new OleDbParameter("@id", OleDbType.Integer,4)
};
			parameters[0].Value = id;

			return DbHelperOleDb.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(ihongma.Model.Order model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into dt_Order(");
			strSql.Append("name,email,tel,zipcode,pcname,[sum],counet,addtiem)");
			strSql.Append(" values (");
			strSql.Append("@name,@email,@tel,@zipcode,@pcname,@sum,@counet,@addtiem)");
			OleDbParameter[] parameters = {
					new OleDbParameter("@name", OleDbType.VarChar,255),
					new OleDbParameter("@email", OleDbType.VarChar,255),
					new OleDbParameter("@tel", OleDbType.VarChar,255),
					new OleDbParameter("@zipcode", OleDbType.VarChar,255),
					new OleDbParameter("@pcname", OleDbType.VarChar,255),
					new OleDbParameter("@sum", OleDbType.VarChar,255),
					new OleDbParameter("@counet", OleDbType.VarChar,255),
					new OleDbParameter("@addtiem", OleDbType.Date)};
			parameters[0].Value = model.name;
			parameters[1].Value = model.email;
			parameters[2].Value = model.tel;
			parameters[3].Value = model.zipcode;
			parameters[4].Value = model.pcname;
			parameters[5].Value = model.sum;
			parameters[6].Value = model.counet;
			parameters[7].Value = model.addtiem;

			int rows=DbHelperOleDb.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(ihongma.Model.Order model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update dt_Order set ");
			strSql.Append("name=@name,");
			strSql.Append("email=@email,");
			strSql.Append("tel=@tel,");
			strSql.Append("zipcode=@zipcode,");
			strSql.Append("pcname=@pcname,");
			strSql.Append("sum=@sum,");
			strSql.Append("counet=@counet,");
			strSql.Append("addtiem=@addtiem");
			strSql.Append(" where id=@id");
			OleDbParameter[] parameters = {
					new OleDbParameter("@name", OleDbType.VarChar,255),
					new OleDbParameter("@email", OleDbType.VarChar,255),
					new OleDbParameter("@tel", OleDbType.VarChar,255),
					new OleDbParameter("@zipcode", OleDbType.VarChar,255),
					new OleDbParameter("@pcname", OleDbType.VarChar,255),
					new OleDbParameter("@sum", OleDbType.VarChar,255),
					new OleDbParameter("@counet", OleDbType.VarChar,255),
					new OleDbParameter("@addtiem", OleDbType.Date),
					new OleDbParameter("@id", OleDbType.Integer,4)};
			parameters[0].Value = model.name;
			parameters[1].Value = model.email;
			parameters[2].Value = model.tel;
			parameters[3].Value = model.zipcode;
			parameters[4].Value = model.pcname;
			parameters[5].Value = model.sum;
			parameters[6].Value = model.counet;
			parameters[7].Value = model.addtiem;
			parameters[8].Value = model.id;

			int rows=DbHelperOleDb.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from dt_Order ");
			strSql.Append(" where id=@id");
			OleDbParameter[] parameters = {
					new OleDbParameter("@id", OleDbType.Integer,4)
};
			parameters[0].Value = id;

			int rows=DbHelperOleDb.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string idlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from dt_Order ");
			strSql.Append(" where id in ("+idlist + ")  ");
			int rows=DbHelperOleDb.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public ihongma.Model.Order GetModel(int id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select id,name,email,tel,zipcode,pcname,sum,counet,addtiem from dt_Order ");
			strSql.Append(" where id=@id");
			OleDbParameter[] parameters = {
					new OleDbParameter("@id", OleDbType.Integer,4)
};
			parameters[0].Value = id;

			ihongma.Model.Order model=new ihongma.Model.Order();
			DataSet ds=DbHelperOleDb.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["id"]!=null && ds.Tables[0].Rows[0]["id"].ToString()!="")
				{
					model.id=int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
				}
				if(ds.Tables[0].Rows[0]["name"]!=null && ds.Tables[0].Rows[0]["name"].ToString()!="")
				{
					model.name=ds.Tables[0].Rows[0]["name"].ToString();
				}
				if(ds.Tables[0].Rows[0]["email"]!=null && ds.Tables[0].Rows[0]["email"].ToString()!="")
				{
					model.email=ds.Tables[0].Rows[0]["email"].ToString();
				}
				if(ds.Tables[0].Rows[0]["tel"]!=null && ds.Tables[0].Rows[0]["tel"].ToString()!="")
				{
					model.tel=ds.Tables[0].Rows[0]["tel"].ToString();
				}
				if(ds.Tables[0].Rows[0]["zipcode"]!=null && ds.Tables[0].Rows[0]["zipcode"].ToString()!="")
				{
					model.zipcode=ds.Tables[0].Rows[0]["zipcode"].ToString();
				}
				if(ds.Tables[0].Rows[0]["pcname"]!=null && ds.Tables[0].Rows[0]["pcname"].ToString()!="")
				{
					model.pcname=ds.Tables[0].Rows[0]["pcname"].ToString();
				}
				if(ds.Tables[0].Rows[0]["sum"]!=null && ds.Tables[0].Rows[0]["sum"].ToString()!="")
				{
					model.sum=ds.Tables[0].Rows[0]["sum"].ToString();
				}
				if(ds.Tables[0].Rows[0]["counet"]!=null && ds.Tables[0].Rows[0]["counet"].ToString()!="")
				{
					model.counet=ds.Tables[0].Rows[0]["counet"].ToString();
				}
				if(ds.Tables[0].Rows[0]["addtiem"]!=null && ds.Tables[0].Rows[0]["addtiem"].ToString()!="")
				{
					model.addtiem=DateTime.Parse(ds.Tables[0].Rows[0]["addtiem"].ToString());
				}
				return model;
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select id,name,email,tel,zipcode,pcname,sum,counet,addtiem ");
			strSql.Append(" FROM dt_Order ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperOleDb.Query(strSql.ToString());
		}

       
        /// <summary>
        ///  分页获取数据列表
        /// </summary>
        /// <param name="PageSize"></param>
        /// <param name="PageIndex"></param>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public DataSet GetList(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * FROM dt_Order");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            recordCount = Convert.ToInt32(DbHelperOleDb.GetSingle(PagingHelper.CreateCountingSql(strSql.ToString())));
            return DbHelperOleDb.Query(PagingHelper.CreatePagingSql(recordCount, pageSize, pageIndex, strSql.ToString(), filedOrder));
        }
		#endregion  Method
	}
}

